package com.wind.taobao.api.trade.trade.convert
{
	import com.wind.taobao.api.trade.trade.model.RefundMessage;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * RefundMessage转换器
	 * @author fxy
	 *
	 */
	public class RefundMessageConvert
	{
		/**
		 * 转换RefundMessage集合
		 */
		public static function convertRefundMessages(value:Object, type:String="json"):ArrayCollection
		{
			var refundMessages:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							refundMessages.addItem(convertRefundMessage((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return refundMessages;
		}

		/**
		 * 转换单个RefundMessage对象
		 * value:XML 或 JSON
		 */
		public static function convertRefundMessage(value:Object, type:String="json", refundMessage:RefundMessage=null):RefundMessage
		{
			if (refundMessage == null)
				refundMessage=new RefundMessage();
			if (value == null)
				return refundMessage;
			if (type == TaobaoConstants.XML)
			{
				refundMessage=convertXMLToRefundMessage(XML(value), refundMessage);
			}
			else
			{
				refundMessage=convertJSONToRefundMessage(value, refundMessage);
			}
			return refundMessage;
		}

		/**
		 * JSON转换为RefundMessage
		 */
		private static function convertJSONToRefundMessage(json:Object, refundMessage:RefundMessage):RefundMessage
		{
			if (json.hasOwnProperty(TaobaoConstants.MESSAGE_ID))
			{
				refundMessage.message_id=json[TaobaoConstants.MESSAGE_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.REFUND_ID))
			{
				refundMessage.refund_id=json[TaobaoConstants.REFUND_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				refundMessage.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.OWNER_ID))
			{
				refundMessage.owner_id=json[TaobaoConstants.OWNER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.OWNER_NICK))
			{
				refundMessage.owner_nick=json[TaobaoConstants.OWNER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.OWNER_ROLE))
			{
				refundMessage.owner_role=json[TaobaoConstants.OWNER_ROLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.CONTENT))
			{
				refundMessage.content=json[TaobaoConstants.CONTENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.PICTURE_URLS))
			{
				refundMessage.picture_urls=json[TaobaoConstants.PICTURE_URLS];
			}
			return refundMessage;
		}



		/**
		 * XML转换为RefundMessage
		 */
		private static function convertXMLToRefundMessage(value:XML, refundMessage:RefundMessage):RefundMessage
		{
			return refundMessage;
		}


	}
}